package com.example.demo.vo;

import io.swagger.annotations.ApiModelProperty;

/**
 * @ClassName: CommonResult
 * @Author : sz
 * @Date :2024/3/2  10:16
 * @Version :1.0
 */
public class CommonResult<T>{

    public static final String HYSTRIX_ERROR_MESSAGE = "请求超时，请稍候再试";
    public static final int SUCCESS_CODE = 0;
    public static final int FAIL_CODE = -1;
    public static final int TIMEOUT_CODE = -2;
    public static final int VISIT_EX_CODE = -3;
    public static final int VALID_EX_CODE = -9;
    public static final int OPERATION_EX_CODE = -10;
    @ApiModelProperty("响应编码:0/200-请求处理成功")
    private Integer code;
    @ApiModelProperty("返回数据")
    private T data;
    @ApiModelProperty("返回消息")
    private String msg;
    @ApiModelProperty("异常消息")
    private String errorMsg;

    public CommonResult(Integer code, T data, String msg) {
        this.errorMsg = "";
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public CommonResult(Integer code, String msg) {
        this.errorMsg = "";
        this.code = code;
        this.msg = msg;
        this.data = this.data;
    }

    public CommonResult(int code, T data, String msg, String errorMsg) {
        this(code, data, msg);
        this.errorMsg = errorMsg;
    }

    public static <E> CommonResult<E> result(int code, E data, String msg) {
        return new CommonResult(code, data, msg);
    }

    public static <E> CommonResult<E> result(int code, E data, String msg, String errorMsg) {
        return new CommonResult(code, data, msg, errorMsg);
    }

    public static <E> CommonResult<E> success(E data) {
        return new CommonResult(0, data, "ok");
    }

    public static CommonResult<Boolean> success() {
        return new CommonResult(0, true, "ok");
    }

    public static <E> CommonResult<E> fail(String msg) {
        return new CommonResult(-1, msg);
    }

    public static <E> CommonResult<E> fail(int code, String msg) {
        return new CommonResult(code, msg);
    }


}
